package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.StrategyCollect;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.StrategyQuery;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/strategys")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyCatalogService catalogService;
    @Autowired
    private IStrategyCollectService strategyCollectService;
    //查询所有的推荐攻略
    @GetMapping
    public Object list(StrategyQuery qo){

        return strategyService.query(qo).getList();
    }
    //查询指定的攻略
    @GetMapping("/{strategyId}")
    public Object get(@PathVariable Long strategyId){
        return strategyService.get(strategyId);
    }
    //大家都在看的回显
    //当前攻略对应的目录
    @GetMapping("/{strategyId}/travels")
    public Object getTravelByStrategyId(@PathVariable Long strategyId){
        return travelService.gettravelsByStrategyId(strategyId);
    }
    //当前攻略下所有攻略分类
    @GetMapping("/{strategyId}/catalogs")
    public Object getCatalogsByStrategyId(@PathVariable Long strategyId){
        return catalogService.getCatalogByStrategyId(strategyId);
    }

    //查询收藏的攻略
    @GetMapping("/{id}/collect")
    public Object selectTavelsByuserId(@PathVariable Long id, StrategyQuery qo){

        return strategyService.selectStrategysByuserId(qo,id);
    }
    //攻略收藏的添加删除
    //收藏
    @PostMapping("/collect")
    public  Object collect(Long strategyId){
        JSONResult jsonResult = new JSONResult();
        try {
            //首先查询点赞记录是否存在该用户的记录
            //得到当前用户的id
            User user = UserContext.getCurrentUser();
            if (user==null){
                jsonResult.setMsg("您未登录");
                jsonResult.setSuccess(false);
                return jsonResult;
            }
            Long userId = user.getId();
           StrategyCollect strategyCollect = strategyCollectService.get(userId, strategyId);

            if (strategyCollect !=null){
                strategyCollectService.delete(strategyCollect.getId());
                jsonResult.setData("加入收藏");
            }else {
                StrategyCollect collect  = new StrategyCollect();
                collect.setUser(user);
                collect.setStrategy(strategyService.getStrategy(strategyId));
                strategyCollectService.save(collect);
                jsonResult.setData("取消收藏");
            }

        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }


        return  jsonResult;
    }


    @GetMapping("/get/{strategyId}")
    public  Object selectCollect(@PathVariable Long strategyId){
        JSONResult jsonResult = new JSONResult();
        try {
            //首先查询点赞记录是否存在该用户的记录
            //得到当前用户的id
            User user = UserContext.getCurrentUser();
            if (user == null) {
                jsonResult.setMsg("您未登录");
                jsonResult.setSuccess(false);
                return jsonResult;
            }
            StrategyCollect strategyCollect = strategyCollectService.get(user.getId(), strategyId);
            jsonResult.setData(strategyCollect);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }

    @GetMapping("/count/{strategyId}")
    public Object selectCount(@PathVariable Long strategyId){
        return strategyCollectService.selectCount(strategyId);
    }



}
